package io.renren.modules.jsf.controller;

import com.alibaba.fastjson.JSONObject;
import io.renren.common.utils.R;
import io.renren.modules.jsf.entity.JsfTotalMoneyEntity;
import io.renren.modules.jsf.entity.vo.PayVO;
import io.renren.modules.jsf.service.JsfTotalMoneyService;
import io.renren.modules.jsf.service.PayService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@RestController
public class PayController {

    @Autowired
    PayService payService;

    @Autowired
    SysUserService userService;

    @Autowired
    JsfTotalMoneyService totalMoneyService;

    @GetMapping(value = "/pay/pay", produces = {"text/html;charset=UTF-8"})
    public Object returnUrl(HttpServletRequest request) throws Exception {
        PayVO payVO = new PayVO();
        SysUserEntity userEntity = userService.selectOneByName(request.getParameter("username"));
        if (userEntity == null) {
            return R.error("账户不存在!");
        }

        payVO.setUsername(request.getParameter("username"));
        payVO.setMoney(Double.valueOf(request.getParameter("money")));

        //入库
        JsfTotalMoneyEntity totalMoneyEntity = new JsfTotalMoneyEntity();
        totalMoneyEntity.setUserId(Math.toIntExact(userEntity.getUserId()));
        totalMoneyEntity.setUsername(userEntity.getUsername());
        totalMoneyEntity.setShouRu(String.valueOf(payVO.getMoney()));
        totalMoneyEntity.setMemo("余额充值");
        totalMoneyEntity.setCreateTime(new Date());
        totalMoneyService.save(totalMoneyEntity);

        return payService.pay(payVO);
    }
}
